#!/usr/bin/python
# -*- coding: UTF-8 -*-
# author: WuJian 2021-11-16
# desc: 公共测试接口定义
import telnetlib
import time

#######################Common Test interface define start!#######################

# AT Cmd send interface
def exec_at_cmd(commands, at_timeout=5, key_str='OK'):
    at_ack = ''
    try:
        tn = telnetlib.Telnet('192.168.50.1', 5504, 5)
        time.sleep(0.4)
        for command in commands:
            result = tn.write(command.encode('ascii') + b'\r\n')
            print(f'send: {command}')
            result = tn.read_until(key_str.encode('ascii'), at_timeout)
            print('recv:', result.decode('ascii'))
            if result is not None:
                result = result.decode('ascii')
                if key_str in result:
                    at_ack += result
                else:
                    print(f'send {command} fail.')
                    at_ack = ''
                    break
        tn.close()
    except Exception as e:
        print("exec_at_cmd telnet error: ", e)
    return at_ack

def sp_print(msg):
    print(f'\033[1;31;36m{msg}\033[0m')
    
def sp_input(msg):
    return input(f'\033[1;31;36m{msg}\033[0m')    

#######################Common Test interface define end!#######################
